/*
 * lexical_cast.h
 *
 *  Created on: 2008-9-26
 *      Author: fanliangliang
 */

#ifndef TEXT_LEXICAL_CAST_H_
#define TEXT_LEXICAL_CAST_H_

#include "toxic.h"
#include <iomanip>
#include <string>

using namespace std;
using namespace toxic; // for lexical_cast

void do_test_lexical_cast() {

	cout << "***** lexical_cast *****" << endl;

	cout << boolalpha;

	string s1 = lexical_cast<string>(45);
	cout << "int to string: " << ("45" == s1) << endl;

	int i1 = lexical_cast<int>("45");
	cout << "string to int: " << (45 == i1) << endl;


	string s2 = lexical_cast<string>(false);
	cout << "bool to string: " << ("false" == s2) << endl;

	bool b1 = lexical_cast<bool>("true");
	cout << "string to bool: " << (true == b1) << endl;


	string s3 = lexical_cast<string>(45.785);
	cout << "double to string: " << ("45.785" == s3) << endl;

	double d1 = lexical_cast<double>("45.785");
	cout << "string to double: " << (45.785 == d1) << endl;

	cout << noboolalpha;

	try {
		int i = lexical_cast<int>("ab");
		cout << i << endl;
	}
	catch (const bad_lexical_cast& e) {
		cout << e.what() << endl;
	}

	// Output:
	// int to string: true
	// string to int: true
	// bool to string: true
	// string to bool: true
	// double to string: true
	// string to double: true
	// bad lexical cast: can not interpret 'string' as 'int' [with value = "ab"]
}

#endif /* TEXT_LEXICAL_CAST_H_ */
